﻿using System;

namespace NetTopologySuite.Utilities
{
	public class HexConverter
	{
		private HexConverter()
		{
		}

		public static string ConvertAny2Any(string valueIn, int baseIn, int baseOut)
		{
			string text = "Error";
			valueIn = valueIn.ToUpper();
			if (baseIn < 2 || baseIn > 36 || baseOut < 2 || baseOut > 36)
			{
				return text;
			}
			if (valueIn.Trim().Length == 0)
			{
				return text;
			}
			if (baseIn == baseOut)
			{
				return valueIn;
			}
			double num = 0.0;
			try
			{
				if (baseIn == 10)
				{
					num = double.Parse(valueIn);
				}
				else
				{
					char[] array = valueIn.ToCharArray();
					int num2 = array.Length;
					for (int i = 0; i < array.Length; i++)
					{
						int num3 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".IndexOf(array[i]);
						if (num3 < 0 || num3 > baseIn - 1)
						{
							return text;
						}
						num2--;
						num += (double)num3 * System.Math.Pow((double)baseIn, (double)num2);
					}
				}
				if (baseOut == 10)
				{
					text = num.ToString();
				}
				else
				{
					text = string.Empty;
					while (num > 0.0)
					{
						int num4 = (int)(num % (double)baseOut);
						num = (num - (double)num4) / (double)baseOut;
						text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".Substring(num4, 1) + text;
					}
				}
			}
			catch (Exception ex)
			{
				text = ex.Message;
			}
			return text;
		}
	}
}
